
#include "area.h"
#include "avatar.h"
#include "commands-aedit.h"
#include "commandTable.h"
#include "commandTable-default.h"
#include "display.h"
#include "handler.h"
#include "room.h"
#include "symphony.h"
#include "terrainTable.h"
#include "world.h"

/*
ACmd::ACmd( void ) {
  name( "" );
  return;
}

bool ACmd::execute( Creature* creature, const std::string& args ) {

  return true;
}
*/

ACmdDelete::ACmdDelete( void ) {
  name( "delete" );
  level( GOD );
  syntax( "syntax: delete delete" );
  return;
}

bool ACmdDelete::execute( Creature* creature, const std::string& args ) {
  Area* area = creature->aedit();
  CmdExit exit;

  if ( args != "delete" ) {
    creature->send( syntax() );
    return false;
  }

  exit.execute( creature, "" );
  creature->send( "\nYou've deleted Area %lu.\n", area->ID() );
  World::Instance().bigBrother( creature, ADMIN_BIGBRO_CHANGES, "%s has deleted area %lu (%s).", creature->name().c_str(), area->ID(), area->name().c_str() );
  area->destroy();

  return true;
}

ACmdInformation::ACmdInformation( void ) {
  name( "information" );
  level( BUILDER );
  return;
}

bool ACmdInformation::execute( Creature* creature, const std::string& args ) {
  Area* area = creature->aedit();
  std::string output;
  char buffer[MAX_BUFFER];
  char names[MAX_BUFFER];
  std::map<unsigned long,std::string> perms;

  for ( std::set<std::pair<unsigned long,unsigned long> >::iterator it = World::Instance().permissions().begin(); it != World::Instance().permissions().end(); ++it ) {
    if ( it->first == area->ID() ) {
      perms[it->second];
    }
  }

  strcpy( names, "{y" );
  if ( perms.empty() ) {
    strcat( names, "none" );
  } else {
    for ( std::map<unsigned long,std::string>::const_iterator it = perms.begin(); it != perms.end(); ++it ) {
      strcat( names, "{y" );
      strcat( names, World::Instance().getAvatarNameByID( it->first ).c_str() );
      strcat( names, "{x, " );
    }
  }
  strcat( names, "{x" );

  sprintf( buffer, "AreaID: {y%lu{x\nFirst vnum: {y%lu{x\nLast vnum: {y%lu{x\nDefault terrain: {y%s{x\nPermission: %s", area->ID(), area->low(), area->high(), area->terrain()->name().c_str(), names );
  output.append( buffer );

  creature->send( output );
  return true;
}

ACmdName::ACmdName( void ) {
  name( "name" );
  level( BUILDER );
  return;
}

bool ACmdName::execute( Creature* creature, const std::string& args ) {
  Avatar* avatar = (Avatar*)creature;
  Area* area = avatar->aedit();
  std::string name = Regex::trim( args );

  area->name( name );
  avatar->send( "You've reset area %d's name to \"{W%s{x\"!", area->ID(), area->name().c_str() );

  return true;
}

ACmdPermission::ACmdPermission( void ) {
  name( "permission" );
  level( GOD );
  syntax( "syntax: permission grant <player>\n        permission revoke <player>\n" );
  return;
}

bool ACmdPermission::execute( Creature* creature, const std::string& args ) {
  std::vector<std::string> pieces = Regex::split( args );
  Avatar* target = NULL;

  if ( ! (creature->admin( ADMIN_HEAD_BUILDER ) || creature->level() >= CREATOR ) ) {
    creature->send( "Sorry, only the Head Builder can fiddle with permissions." );
    return false;
  }

  if ( pieces.size() == 2 ) {
    if ( pieces[0] == "grant" ) {
      if ( ( target = World::Instance().findAvatar( pieces[1] ) ) == NULL ) {
        creature->send( "They couldn't be found." );
        return false;
      }
      if ( World::Instance().hasPermission( creature->aedit(), target ) ) {
        creature->send( "%s already has permission to %s.", target->name().c_str(), creature->aedit()->name().c_str() );
        return false;
      }
      World::Instance().givePermission( creature->aedit(), target );
      creature->send( "You've {Ggranted{x %s access to %s.", target->name().c_str(), creature->aedit()->name().c_str() );
      target->send( "%s has {Ggranted{x you access to %s.", target->seeName( creature, true ).c_str(), creature->aedit()->name().c_str() );
    } else if ( pieces[0] == "revoke" ) {
      if ( ( target = World::Instance().findAvatar( pieces[1] ) ) == NULL ) {
        creature->send( "They couldn't be found." );
        return false;
      }
      if ( !World::Instance().hasPermission( creature->aedit(), target ) ) {
        creature->send( "%s doesn't have permission to %s.", target->name().c_str(), creature->aedit()->name().c_str() );
        return false;
      }
      World::Instance().removePermission( creature->aedit(), target );
      creature->send( "You've {rrevoked{x %s's access to %s.", target->name().c_str(), creature->aedit()->name().c_str() );
      target->send( "%s has {rrevoked{x your access to %s.", target->seeName( creature, true ).c_str(), creature->aedit()->name().c_str() );
    } else {
      creature->send( syntax() );
      return false;
    }
  } else {
    creature->send( syntax() );
    return false;
  }

  return true;
}

ACmdTerrain::ACmdTerrain( void ) {
  name( "terrain" );
  level( BUILDER );
  syntax( "syntax: terrain\n        terrain <type>" );
  return;
}

bool ACmdTerrain::execute( Creature* creature, const std::string& args ) {
  std::vector<std::string> pieces = Regex::split( args, 1 );

  if ( args.empty() ) {
    creature->send( "Currently supported terrains:\n%s", TerrainTable::Instance().list().c_str() );
    return true;
  }

  if ( pieces.size() == 1 ) {
    creature->aedit()->terrain( TerrainTable::Instance().find( pieces[0] ) );
    creature->send( "Area default terrain set to {%c%s{x.", creature->aedit()->terrain()->title(), creature->aedit()->terrain()->name().c_str() );
  } else {
    creature->send( syntax() );
    return false;
  }

  return true;
}

